

********************************************************************************************
* This file constructs the panel of individual data and merges in the household-level data
********************************************************************************************

*\ Structure of File:
*
* 1. Load most recent individual file (contains all current/historic heads)
* 2. Rename relevant variables
* 3. Attach to households
*
********************************************************************************************
* 1. Load Individual file 
********************************************************************************************


cd "$output"

* Load the individual file
u "$data/2017IND.dta", clear

#delimit;
keep  	ER30001	ER30002 /* 1968 Family ID and Sequence # which can be used for unique id */

		ER30020 ER30067 ER30117 ER30160 
		ER30217 ER30283 ER30343 ER30399 
		ER30429 ER30463 ER30498 ER30535 ER30570 
		ER30606 ER30642 ER30689 ER30733 ER30806 
		ER33101 ER33201 ER33301 ER33401 ER33501 
		ER33601 ER33701 ER33801 ER33901 ER34001 
		ER34101 ER34201 ER34301 ER34501 

										/* Interview number */
		
        ER30021 ER30044 ER30068 ER30092 ER30118 ER30139 ER30161 ER30189 ER30218 
		ER30247 ER30284 ER30314 ER30344 ER30374 ER30400 ER30430 ER30464 
		ER30536 ER30607 ER30690 ER30807 ER33202 ER33402 ER33502	ER33602 ER33702 
		ER33802 ER33902 ER34002	ER34102 ER34202 ER34302 ER34502 ER30500 ER30572
		ER30644 ER30735 ER33103 ER33303 
		/* Sequence number */
		
      	ER30022 ER30069 ER30119 ER30162 ER30219 ER30285 ER30345 ER30401 ER30465 
		ER30537 ER30608 ER30691 ER30808 ER33203 ER33403 ER33503 ER33603 ER33703
		ER33803 ER33903	ER34003 ER34103 ER34203 ER34303 ER34503 ER30499 ER30571 
		ER30643 ER30734 ER33102 ER33302
		/* Relationship to Head */
		
		/*add in vars  I need here from individual file*/
		ER34030 ER34137 ER34244 ER34394  ER34603 /* DI receipt */
		ER30404 ER30434 ER30468 ER30503 ER30540 ER30575 
		ER30611 ER30647 ER30694 ER30738 ER30811 ER33106 
		ER33206 ER33306 ER33406 ER33506 ER33606 ER33706 
		ER30520 ER30556 ER30591 ER30627 ER30664 ER30712 ER30757 /* DI status */

		ER33806 ER33906 ER34006 ER34106 ER34206 ER34307 ER34506 /* birth year */
		
		ER30681 ER30724 ER30799 ER30859 ER33125 ER33281 
		ER33322 ER33434 ER33542 ER33633 ER33736 ER33842 
		ER33942 ER34036 ER34148 ER34255 ER34405 ER34644 /* follow status*/
		
		/*current employment*/
		ER30293 ER30323 ER30353 ER30382 ER30411 ER30441 ER30474 ER30509 ER30545
		ER30580 ER30616 ER30653 ER30699 ER30744 ER30816 ER33111 ER33211 ER33311
		ER33411 ER33512 ER33612 ER33712 ER33813 ER33913 ER34016 ER34116 ER34216
		ER34317 ER34516
		
		ER33506 ; /* Year individual born + education*/
		
#delimit cr

* Generate uniqe identifier
gen persid = (ER30001*1000) + ER30002

* Drop Latino households
gen latino = ER30001>=7001 & ER30001<=9308 

* Dummy for SEO sample
gen seo=ER30001>=5000 & ER30001<=7000
/*
* Drop individuals who are never household heads
drop if ER33503!=10 & ER33603!=10 & ER33703!=10 & ER33803!=10 & ER33903!=10 ///
& ER34003!=10 & ER34103!=10	& ER34203!=10 & ER34303!=10	& ER34503!=10 ///
& ER30022!=10 & ER30069!=10 & ER30119!=10 & ER30162!=10 ///
& ER30219!=10 & ER30285!=10 & ER30345!=10 & ER30401!=10 ///
& ER30465!=10 & ER30537!=10 & ER30608!=10 & ER30691!=10 ///
& ER30808!=10 & ER33203!=10 & ER33403!=10 & ER33403!=10
*/
* Rename hh interview # as yearly ID
ren ER30020 id1969
ren ER30067 id1971
ren ER30117 id1973
ren ER30160 id1975
ren ER30217 id1977
ren ER30283 id1979
ren ER30343 id1981
ren ER30399 id1983
ren ER30429 id1984
ren ER30463 id1985
ren ER30498 id1986
ren	ER30535 id1987
ren ER30570 id1988
ren ER30606 id1989
ren ER30642 id1990
ren ER30689 id1991
ren ER30733 id1992
ren ER30806 id1993
ren ER33101 id1994
ren ER33201 id1995
ren ER33301 id1996
ren ER33401 id1997
ren ER33501 id1999
ren ER33601 id2001
ren ER33701 id2003
ren ER33801 id2005
ren ER33901 id2007
ren ER34001 id2009
ren ER34101 id2011
ren ER34201 id2013
ren ER34301 id2015
ren ER34501 id2017

* Generate dummy for HH each year
gen pid1969=ER30022==1 & ER30021>=1 & ER30021<=20
gen pid1971=ER30069==1 & ER30068>=1 & ER30068<=20
gen pid1973=ER30119==1 & ER30118>=1 & ER30118<=20
gen pid1975=ER30162==1 & ER30161>=1 & ER30161<=20
gen pid1977=ER30219==1 & ER30218>=1 & ER30218<=20
gen pid1979=ER30285==1 & ER30284>=1 & ER30284<=20
gen pid1981=ER30345==1 & ER30344>=1 & ER30344<=20
gen pid1983=ER30401==10 & ER30400>=1 & ER30400<=20
gen pid1985=ER30465==10 & ER30464>=1 & ER30464<=20
gen pid1987=ER30537==10 & ER30536>=1 & ER30536<=20
gen pid1989=ER30608==10 & ER30607>=1 & ER30607<=20
gen pid1991=ER30691==10 & ER30690>=1 & ER30690<=20
gen pid1993=ER30808==10 & ER30807>=1 & ER30807<=20
gen pid1995=ER33203==10 & ER33202>=1 & ER33202<=20
gen pid1997=ER33403==10 & ER33402>=1 & ER33402<=20
gen pid1999=ER33503==10 & ER33502>=1 & ER33502<=20
gen pid2001=ER33603==10 & ER33602>=1 & ER33602<=20
gen pid2003=ER33703==10 & ER33702>=1 & ER33702<=20
gen pid2005=ER33803==10 & ER33802>=1 & ER33802<=20
gen pid2007=ER33903==10 & ER33902>=1 & ER33902<=20
gen pid2009=ER34003==10 & ER34002>=1 & ER34002<=20
gen pid2011=ER34103==10 & ER34102>=1 & ER34102<=20
gen pid2013=ER34203==10 & ER34202>=1 & ER34202<=20
gen pid2015=ER34303==10 & ER34302>=1 & ER34302<=20
gen pid2017=ER34503==10 & ER34502>=1 & ER34502<=20
gen pid1986=ER30500 ==10 & ER30499 >=1 & ER30499 <=20
gen pid1988=ER30572 ==10 & ER30571 >=1 & ER30571 <=20
gen pid1990=ER30644 ==10 & ER30643 >=1 & ER30643 <=20
gen pid1992=ER30735 ==10 & ER30734 >=1 & ER30734 <=20
gen pid1994=ER33103 ==10 & ER33102 >=1 & ER33102 <=20
gen pid1996=ER33303 ==10 & ER33302 >=1 & ER33302 <=20

*Generate dummy for spouse each year
gen sp_pid1969=ER30022==2 & ER30021>=1 & ER30021<=20
gen sp_pid1971=ER30069==2 & ER30068>=1 & ER30068<=20
gen sp_pid1973=ER30119==2 & ER30118>=1 & ER30118<=20
gen sp_pid1975=ER30162==2 & ER30161>=1 & ER30161<=20
gen sp_pid1977=ER30219==2 & ER30218>=1 & ER30218<=20
gen sp_pid1979=ER30285==2 & ER30284>=1 & ER30284<=20
gen sp_pid1981=ER30345==2 & ER30344>=1 & ER30344<=20
gen sp_pid1983=ER30401>=20 & ER30401<30 & ER30401<30 & ER30400>=1 & ER30400<=20
gen sp_pid1985=ER30465>=20 & ER30465<30 & ER30464>=1 & ER30464<=20
gen sp_pid1987=ER30537>=20 & ER30537<30 & ER30536>=1 & ER30536<=20
gen sp_pid1989=ER30608>=20 & ER30608<30 & ER30607>=1 & ER30607<=20
gen sp_pid1991=ER30691>=20 & ER30691<30 & ER30690>=1 & ER30690<=20
gen sp_pid1993=ER30808>=20 & ER30808<30 & ER30807>=1 & ER30807<=20
gen sp_pid1995=ER33203>=20 & ER33203<30 & ER33202>=1 & ER33202<=20
gen sp_pid1997=ER33403>=20 & ER33403<30 & ER33402>=1 & ER33402<=20
gen sp_pid1999=ER33503>=20 & ER33503<30 & ER33502>=1 & ER33502<=20
gen sp_pid2001=ER33603>=20 & ER33603<30 & ER33602>=1 & ER33602<=20
gen sp_pid2003=ER33703>=20 & ER33703<30 & ER33702>=1 & ER33702<=20
gen sp_pid2005=ER33803>=20 & ER33803<30 & ER33802>=1 & ER33802<=20
gen sp_pid2007=ER33903>=20 & ER33903<30 & ER33902>=1 & ER33902<=20
gen sp_pid2009=ER34003>=20 & ER34003<30 & ER34002>=1 & ER34002<=20
gen sp_pid2011=ER34103>=20 & ER34103<30 & ER34102>=1 & ER34102<=20
gen sp_pid2013=ER34203>=20 & ER34203<30 & ER34202>=1 & ER34202<=20
gen sp_pid2015=ER34303>=20 & ER34303<30 & ER34302>=1 & ER34302<=20
gen sp_pid2017=ER34503>=20 & ER34503<30 & ER34502>=1 & ER34502<=20
gen sp_pid1986=ER30500>=20 & ER30500<30 & ER30499 >=1 & ER30499 <=20
gen sp_pid1988=ER30572>=20 & ER30572<30 & ER30571 >=1 & ER30571 <=20
gen sp_pid1990=ER30644>=20 & ER30644<30 & ER30643 >=1 & ER30643 <=20
gen sp_pid1992=ER30735>=20 & ER30735<30 & ER30734 >=1 & ER30734 <=20
gen sp_pid1994=ER33103>=20 & ER33103<30 & ER33102 >=1 & ER33102 <=20
gen sp_pid1996=ER33303>=20 & ER33303<30 & ER33302 >=1 & ER33302 <=20


ren ER34030 DI2009
ren ER34137 DI2011
ren ER34244 DI2013
ren ER34394 DI2015
ren ER34603 DI2017

ren ER30404  birthyear1983
ren ER30434  birthyear1984
ren ER30468  birthyear1985
ren ER30503  birthyear1986
ren ER30540  birthyear1987
ren ER30575  birthyear1988
ren ER30611  birthyear1989
ren ER30647  birthyear1990
ren ER30694  birthyear1991
ren ER30738  birthyear1992
ren ER30811  birthyear1993
ren ER33106  birthyear1994
ren ER33206  birthyear1995
ren ER33306  birthyear1996
ren ER33406  birthyear1997
ren ER33506  birthyear1999
ren ER33606  birthyear2001
ren ER33706  birthyear2003
ren ER33806  birthyear2005
ren ER33906  birthyear2007
ren ER34006  birthyear2009
ren ER34106 birthyear2011
ren ER34206 birthyear2013
ren ER34307 birthyear2015
ren ER34506 birthyear2017

ren ER30681 sampleperson1990
ren ER30724 sampleperson1991
ren ER30799 sampleperson1992
ren ER30859 sampleperson1993
ren ER33125 sampleperson1994
ren ER33281 sampleperson1995
ren ER33322 sampleperson1996
ren ER33434 sampleperson1997
ren ER33542 sampleperson1999
ren ER33633 sampleperson2001
ren ER33736 sampleperson2003
ren ER33842 sampleperson2005
ren ER33942 sampleperson2007
ren ER34036 sampleperson2009
ren ER34148 sampleperson2011
ren ER34255 sampleperson2013
ren ER34405 sampleperson2015
ren ER34644 sampleperson2017


*Keeping the sequence numbers, for identifying when people leave the household and PSID
*BUT assigning them to the previous year, last one in which the person was observed
tokenize 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1999 2001 2003 2005 2007 2009 2011 2013 2015 
foreach x in  ER30044 ER30068 ER30092 ER30118 ER30139 ER30161 ER30189 ER30218 ER30247 ER30284 ER30314 ER30344 ER30374 ER30400 ER30430 ER30464 ER30499 ER30536 ER30571 ER30607 ER30643 ER30690 ER30734 ER30807 ER33102 ER33202 ER33302 ER33402 ER33502 ER33602 ER33702 ER33802 ER33902 ER34002 ER34102 ER34202 ER34302  ER34502 {
gen sample_leavenext`1' = `x'>70 & `x' <=80 
gen sample_dienext`1' = `x'>80 & `x' <90 
macro shift
}

*CURRENT employment status:
tokenize 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1999 2001 2003 2005 2007 2009 2011 2013 2015 2017
foreach x in ER30293 ER30323 ER30353 ER30382 ER30411 ER30441 ER30474 ER30509 ER30545 ER30580 ER30616 ER30653 ER30699 ER30744 ER30816 ER33111 ER33211 ER33311 ER33411 ER33512 ER33612 ER33712 ER33813 ER33913 ER34016 ER34116 ER34216 ER34317 ER34516 {
gen empnow`1' = `x'
macro shift
}

tokenize 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1999 2001 2003 2005 2007 2009 2011 2013 2015 2017
foreach x in  ER30021 ER30044 ER30068 ER30092 ER30118 ER30139 ER30161 ER30189 ER30218 ER30247 ER30284 ER30314 ER30344 ER30374 ER30400 ER30430 ER30464 ER30499 ER30536 ER30571 ER30607 ER30643 ER30690 ER30734 ER30807 ER33102 ER33202 ER33302 ER33402 ER33502 ER33602 ER33702 ER33802 ER33902 ER34002 ER34102 ER34202 ER34302  ER34502 {
gen seqno`1'=`x'
macro shift
}

tokenize 1986 1987 1988 1989 1990 1991 1992
foreach x in ER30520 ER30556 ER30591 ER30627 ER30664 ER30712 ER30757 {
gen typen_indiv`1' = `x'
macro shift
}

*Keep relevant variables
keep id* latino* sp_pid* pid* seo persid DI* birthyear* sample_leave* sample_die* seqno* sampleperson* typen_indiv* empnow*

* Reshape into panel data format
gen person=1
replace person=sum(person)

reshape long id pid sp_pid DI birthyear sample_leavenext sample_dienext seqno  sampleperson typen_indiv empnow,i(person) j(year)

bysort id year: egen coupled = max(sp_pid)
drop sp_pid
replace birthyear =. if birthyear==0 | birthyear==9999
egen birthy=mode(birthyear),by(person) min

* Drop any individual in any year for which they are no head of household
*or too early in dataset
*AND they are not leaving the PSID family unit
drop if pid==0 | pid==.

/* TO DO - ADD HEAD'S EDUCATION
ren educ educ_ind
replace educ=. if educ>=98 | educ==0 	
label var educ_ind "head's education from the individual file"
*/

* Label person id
label var person "UNIQUE PERSON IDENTIFER (HH HEADS ONLY)" 

compress


save "$temp\I_Panel.dta", replace

replace year = year-1
merge 1:1 id year using "$intermediary\transfer_DI_data.dta", update replace
*The only unmatched guys are before 1986
*updating typen for values 1992-2008

drop if _merge< 3
drop _merge

by person, sort: gen numwav=_N

sort numwav person year 

replace year = year+1

merge 1:1 id year using "$intermediary\HH_Panel.dta", update replace

drop _merge

sort person year
gen wifeid = wifechange==1
bysort person: replace wifeid=sum(wifeid)
egen sp_person =group(person wifeid)



gen     sp_DS=2 if 		((sp_disab==1 & sp_twdisab==7)|(sp_disab==1 & sp_twdisab==1 & sp_hmdisab==1)|(sp_disab==1 & sp_twdisab==5 & sp_hmdisab==1)| ///
						(sp_disab==1 & sp_twdisab==8 & sp_hmdisab==1))
replace sp_DS=1 if 		((sp_disab==1 & sp_twdisab==1 & sp_hmdisab==3)|(sp_disab==1 & sp_twdisab==8 & sp_hmdisab==3)|(sp_disab==1 & sp_twdisab==5 & sp_hmdisab==3)| ///
						(sp_disab==1 & sp_twdisab==1 & sp_hmdisab==5)|(sp_disab==1 & sp_twdisab==8 & sp_hmdisab==5)|(sp_disab==1 & sp_twdisab==5 & sp_hmdisab==5))
replace sp_DS=0 if 		sp_DS==.

gen     DS=2 if 		((disab==1 & twdisab==7)|(disab==1 & twdisab==1 & hmdisab==1)|(disab==1 & twdisab==5 & hmdisab==1)| ///
						(disab==1 & twdisab==8 & hmdisab==1))
replace DS=1 if 		((disab==1 & twdisab==1 & hmdisab==3)|(disab==1 & twdisab==8 & hmdisab==3)|(disab==1 & twdisab==5 & hmdisab==3)| ///
						(disab==1 & twdisab==1 & hmdisab==5)|(disab==1 & twdisab==8 & hmdisab==5)|(disab==1 & twdisab==5 & hmdisab==5))
replace DS=0 if 		DS==.


replace DI = . if DI >7
replace typen = . if typen >7
replace typen_indiv = . if typen_indiv >7

replace DI = DI ==1 if DI != .
replace DI = typen==1 if DI == . & typen != .

replace sp_DI = . if sp_DI > 7
replace sp_typen = . if sp_typen > 7

replace sp_DI = sp_DI == 1 if sp_DI != .
replace sp_DI = sp_typen==1 if sp_DI ==. & sp_typen != .

replace empnow = empnow == 1 if empnow != .

*******Makes some adjustment to the DI definition ***********************

egen lastyear=max(year),by(person)
sort person year

/* typen==4 is "Any combination of 1. Disability, 2.  Retirement, 3.  Survivor's benefits; dependent of deceased recipient, */
/* 5.  Dependent of disabled recipient, 6.  Dependent of retired recipient, and 7.  Other"*/                                               

/* The assumption is that a sequence DI=1,DI=0, DI=1 with typen=4 when DI reverts to 0 just means that in that year the person was receiving DI *and* something else */ 
qby person:replace DI=1 if DI==0 & typen==4 & DI[_n-1]==1 & DI[_n+1]==1		
/*Assume a sequence <<1 0 end>> with typen=4 when DI=0 is a false exit */
qby person:replace DI=1 if DI==0 & typen==4 & DI[_n-1]==1 & year==lastyear		
/*Assume a sequence <<1 0 whatever>> with typen=4 when DI=0 is a false exit */
qby person:replace DI=1 if DI==0 & typen==4 & DI[_n-1]==1				    

egen ndi=sum(DI), by(person)
egen nDS=sum(DS==0),by(person)
egen nDI=sum(DI==0),by(person)
replace DI=0 if DI==1 & year<lastyear & ndi==1 & (nDS-nDI)==1				/*Assumes that if the guy reports DI only 1 year and he's always non disabled, that's a false entry*/
drop  ndi nDS nDI


**ADJUSTING TRANSFERS:
*imputed values that need to be corrected:
foreach x of global tr_general_acc_othervars {
foreach y of global tr_general_acc_years {
replace `x' = . if tr_general_acc != 0 & year == `y'
}
}

foreach x in tr_unem tr_unemw tr_comp  {
replace `x'raw = . if `x'raw >= 999997
replace `x'raw = . if `x'raw >= 99997 & year >= 1993 & year <= 2003
replace `x'raw = . if (`x'per > 6 & `x'per != . & year != 1995 & year != 1994) | (`x'per> 4 & `x'per != . & (year == 1995 | year == 1994))
replace `x' =. if `x' >=999997
replace `x' =. if `x' >= 99997 & year <= 2003
replace `x'raw = `x'raw * 52 if (`x'per == 3 & year != 1995 & year != 1994) | ( `x'per == 1 & (year == 1995 | year == 1994))
replace `x'raw = `x'raw * 26 if (`x'per == 4 & year != 1995 & year != 1994) | ( `x'per == 2 & (year == 1995 | year == 1994))
replace `x'raw = `x'raw * 12 if (`x'per == 5 & year != 1995 & year != 1994) | ( `x'per == 3 & (year == 1995 | year == 1994))
}

replace tr_comp = tr_compraw if year >= 1993 & year <= 2005
replace tr_unem = tr_unemraw if year >= 1993 & year <= 2005
replace tr_unemw = tr_unemwraw if year >= 1993 & year <= 2005

replace tr_comp = . if tr_comp_acc != 0 & tr_comp_acc != .
replace tr_unem = . if tr_unem_acc != 0 & tr_unem_acc != .
replace tr_unemw = . if tr_unemw_acc != 0 & tr_unemw_acc != .

drop tr_compraw tr_compper tr_unemraw tr_unemper tr_unemwraw tr_unemwper

replace fstmp = fstmp2 if year <= 1992
drop fstmp2 fstamp

* Pre-2005 social security is coded jointly
replace soc_sectot = soc_secw + soc_sec + soc_secyoth if soc_sectot==.

drop soc_sec soc_secw soc_secyoth
ren soc_sectot soc_sec

replace soc_sec = . if soc_sec == 999999
replace sp_lw = . if sp_lw >=99
replace sp_lw = ln(sp_lw)



ren tr_comp wcomp
ren fstmp tr_fs
ren tr_unem uihead
ren tr_unemw uiwife

*Adjusting income measures:


*making an all-but-head measure of income:
gen nonhy = tyoth+wly

*making nonlabor income variable:
gen nlyhw=tyhw-wly-ly

*trhw excludes SS income from 1994 onward. In survey year 1993 and before, 
*it includes them.
replace trhw = trhw+soc_sec if year>=1994

*all transfers except for social security
gen trall_nonss = trhw+troth-soc_sec

* Code race consistently across waves
gen race=.
ren race old_race

* Race: 1 is white, 2 black, 3 others
replace race1=old_race if race1==. & old_race!=.

forvalues i=1/4 {
	gen     rc=race`i' if race`i'<=2
	replace rc=3    if race`i'>=3 & race`i'<=7
	drop race`i'
	ren rc race`i'

	gen     wrc=wrace`i' if wrace`i'<=2
	replace wrc=3    if wrace`i'>=3 & wrace`i'<=7
	drop wrace`i'
	ren wrc wrace`i'
}


gen race=race1
replace race=3 if race2!=0 & race2!=race1		/* Any case of more than one reported race is also considered "other" */ 
gen wrace = wrace1
replace wrace = 3 if wrace2!=0 & wrace2!=wrace1

drop *race1 *race2 *race3 *race4 
bysort person: egen race2=mode(race), minmode
replace race = race2
drop race2

gen white=race==1
gen married=marit==1
gen sev=DS==2
gen mod=DS==1


*******************SPENDING CATEGORIES**************
*generating food spending
tokenize fdhmpER1 fdhmpER2 fddelpER1 fddelpER2 foutpER1 foutpER2
foreach x in fdhm1 fdhm2 fddel1 fddel2 fout1 fout2 {
replace `x' =. if `x' >= 99997 & `x' != .
replace `x' = `x' * 365 if (`1' == 2 & year != 1994) 
replace `x' = `x' * 52 if (`1' == 3 & year != 1994) | ( `1' == 1 & (year == 1994))
replace `x' = `x' * 26 if (`1' == 4 & year != 1994) | ( `1' == 2 & (year == 1994))
replace `x' = `x' * 12 if (`1' == 5 & year != 1994) | ( `1' == 3 & (year == 1994))
replace `x' = . if (`1' > 6 & `1' != . & year != 1994) | (`1'> 4 & `1' != . & (year == 1994))
macro shift
}

replace fdhm1 = . if (fdhm1_imp!=0 & fdhm1_imp!=.)
replace fdhm3 = . if (fdhm3_imp!=0 & fdhm3_imp!=.)

drop food_out
egen totfood = rowtotal(fout3 fdhm3 fdhm2 fdhm1 fddel1 fddel2), mi
egen food_out = rowtotal(fout1 fout2), mi

*rent spending:
replace rent_raw = . if  rent_acc == 1
foreach x in rent_  {
replace `x'raw = . if `x'raw >= 999997
replace `x'raw = . if `x'raw == 99998 | `x'raw == 99999 | `x'raw == 99997
replace `x'raw = . if (`x'raw == 9998 | `x'raw == 9999 | `x'raw == 9997) & year==1993
replace `x'raw = . if (`x'per > 6 & `x'per != . )
replace `x'raw = `x'raw * 365 if (`x'per == 2 & year != 1995 & year != 1994) 
replace `x'raw = `x'raw * 52 if (`x'per == 3 & year != 1995 & year != 1994) | ( `x'per == 3 & (year == 1995 | year == 1994))
replace `x'raw = `x'raw * 26 if (`x'per == 4 & year != 1995 & year != 1994) | ( `x'per == 2 & (year == 1995 | year == 1994))
replace `x'raw = `x'raw * 12 if (`x'per == 5  & year != 1994) | ( `x'per == 1 & (year == 1995 | year == 1994))
}
replace rent = rent_raw if rent == . 

*aggregating spending categories:
ren otheru_raw miscutils_raw
ren auto_ins_raw carins_raw
ren bus_train_raw busfare_raw
ren taxi_raw taxifare_raw
ren other_trans_raw other_trans_raw
ren health_ins_raw hinsurance_raw
ren auto_repair_raw carrepair

replace hinsurance_raw = . if  hinsurance_raw >=  99997
replace hinsurance = . if hinsurance >= 999997

drop telecom
foreach x in water gas electric telecom {
gen `x' = `x'_raw
replace `x' = . if `x' >= 9997
replace `x' = `x'*52 if `x'_per == 3
replace `x' = `x'*26 if `x'_per == 4
replace `x' = `x'*12 if `x'_per == 5
replace `x' = . if `x'_per >= 7 & `x'_per <= 9
}

foreach x in miscutils {
gen `x' = `x'_raw
replace `x' = . if `x' >= 997
replace `x' = `x'*12 if `x'_per == 5
replace `x' = . if `x'_per >=7 & `x'_per <= 9
}



foreach x in hinsurance nurse {
replace `x' = . if `x' >= 999997
replace `x' = `x'/2 if year <= 2011
}
local x hinsurance
replace `x' = `x'_raw*12 if `x'per ==5
replace `x' = `x'_raw*26 if `x'per ==4
replace `x' = `x'_raw*52 if `x'per ==3
replace `x' = . if `x'per >6 & `x'per <=9

drop hinsuranceper hinsurance_raw
*hinsurance, telecom, gas, electric, carins has per variable
*NOT nurse, doctor, prescription, carrepair

replace inst_medical_raw = . if inst_medical_raw >= 999997
replace nurse = inst_medical_raw if nurse ==.
drop inst_medical_raw inst_medical

foreach x in doctor  prescription{
replace `x' = . if `x'_raw >= 999997 & `x'_raw != .
replace `x' = . if `x' >= 999997
replace `x' = `x'/2 if year <= 2013
drop `x'_raw
}

foreach x in carins {
gen `x' = `x'_raw
replace `x' = . if `x' >= 999997
replace `x' = `x'*12 if `x'per == 5
replace `x' = . if `x'per >=7 & `x'per <=9
}
drop carins_raw carinsper

foreach x in carrepair {
replace `x' = . if `x' >= 99997
replace `x' = `x'*12 if year <2015 /*car repairs are monthly before 2015*/
}

drop gasoline parking  other_trans
foreach x in gasoline parking busfare taxifare other_trans {
gen `x'=12*`x'_raw if `x'_raw <= 99997 /*variable is monthly*/
drop `x'_raw
}

egen util=  rowtotal(electric gas water miscutils telecom), mi
egen health=  rowtotal(hinsurance nurse doctor prescription), mi
egen transport=  rowtotal(carins carrepair gasoline parking busfare taxifare other_trans), mi

ren gas heating
replace homeinsure = . if homeinsure_acc > 0 & homeinsure_acc != .

drop childcare
ren childcare_raw childcare
replace childcare = . if childcare >= 99997

replace education = . if education_acc != 0 & education_acc !=.
drop education_acc education_raw*

drop rent rent_acc
ren rent_raw rent

drop homeinsure
rename homeinsure_raw homeinsure
replace homeinsure = . if homeinsure >= 9997
drop homeinsure_acc


replace home_equity = . if home_equity_acc != 0 & home_equity_acc != .
replace bus_ass = . if bus_ass_acc != 0 & bus_ass_acc != .
replace cash = . if cash_acc != 0 & cash_acc != .
replace real_estate = . if real_estate_acc != 0 & real_estate_acc != .
replace stocks  = . if stocks_acc != 0 & stocks_acc != .
replace vehicles  = . if vehicles_acc != 0 & vehicles_acc != .
replace other_ass  = . if other_ass_acc != 0 & other_ass_acc != .
replace ira  = . if ira_acc != 0 & ira_acc != .
replace other_debt  = . if other_debt_acc != 0 & other_debt_acc != .
replace w_with_h  = . if w_with_h_acc != 0 & w_with_h_acc != .

gen w_no_bus = w_with_h - bus_ass
gen w_no_h = w_with_h - home_equity


******************SIMPLIFIED TAX SIMULATION***************

*making a post-tax wealth measure
*TAX INCOME, 1996 parameters:
*NOTE: taxly is CPI-adjusted to 1996
gen totly = wly+ly if married==1
replace totly = ly if married==0

*Two procedures for making SS taxable:
*1) like normal progressive taxes if little work income. Start at 25%, then 85%
*2) more convoluted, if large work income. All SS income is 50% taxable, but 
*earning more money/receiving more SS income exposes previous SS income to 85%
*tax rate as well.
gen ss_taxable = max((soc_sec/2+ly+wly - 32000)*0.5,0) if soc_sec/2+ly+wly <= 44000 & married==1
*below condition implies a range where more SS income exposes previous SS income
*to tax liability, if you have work income in the household
replace ss_taxable = (12000)*0.5 if soc_sec/2+ly+wly > 44000  & married==1 
replace ss_taxable = soc_sec/2 if soc_sec/2 < ss_taxable & ss_taxable !=. & married==1 
replace ss_taxable = ss_taxable + 0.85*(soc_sec/2+ly+wly - 44000) if soc_sec/2+ly+wly > 44000 & married==1
replace ss_taxable = soc_sec*0.85 if soc_sec*0.85 < ss_taxable & ss_taxable != . & married==1

replace ss_taxable = max((soc_sec/2+ly - 25000)*0.5,0) if soc_sec/2+ly <= 34000 & married==0
*below condition implies a range where more SS income exposes previous SS income
*to tax liability, if you have work income in the household
replace ss_taxable = (9000)*0.5 if soc_sec/2+ly+wly > 34000  & married==0 
replace ss_taxable = soc_sec/2 if soc_sec/2 < ss_taxable & ss_taxable !=. & married==0 
replace ss_taxable = ss_taxable + 0.85*(soc_sec/2+ly - 34000) if soc_sec/2+ly > 34000 & married==0
replace ss_taxable = soc_sec*0.85 if soc_sec*0.85 < ss_taxable & ss_taxable != . & married==0

gen taxy = totly+ ss_taxable

*married get dependent deductions:
gen deduct=6700+2550*(2+kids) if married==1
*nonmarried no kids:
replace deduct=4000 +2550 if married==0 & kids ==0
*nonmarried with kids, is head of household:
replace deduct = 5900 +2550 if married == 0 & kids > 0 & kids != .
*partner in non-married household, single:

gen taxly = max(taxy-deduct,0)
gen taxwly = max(wly - 4000-2550,0) if married==0
replace taxwly = 0 if taxwly == .

*married, joint federal taxes on income:
gen taxsim =max((taxly)*0.15,0) if married==1 & taxly<=40100
replace taxsim =(40100)*0.15+(taxly-40100)*0.28 if married==1 & taxly>=40100 & taxly<= 96900
replace taxsim =(40100)*0.15+(96900-40100)*0.28 + (taxly-96900)*0.31 if married==1 & taxly >= 96900& taxly<147700
replace taxsim =(40100)*0.15+(96900-40100)*0.28 + (147700-96900)*0.31 + (taxly-147700)*0.36 if married==1 & taxly>= 147700& taxly<263750
replace taxsim =(40100)*0.15+(96900-40100)*0.28 + (147700-96900)*0.31 + (263750-147700)*0.36 + (taxly-263750)*0.396 if married==1 & taxly>= 263750

*unmarried w/ single filing status, head taxed alone:
replace taxsim =max((taxly)*0.15,0) if married==0 & taxly<=24000 & kids==0
replace taxsim =(24000)*0.15+(taxly-24000)*0.28 if married==0 & taxly>=24000& taxly<= 58150 & kids==0
replace taxsim =(24000)*0.15+(58150-24000)*0.28 + (taxly-58150)*0.31 if married==0 & taxly >= 58150& taxly<121300 & kids==0
replace taxsim =(24000)*0.15+(58150-24000)*0.28 + (121300-58150)*0.31 + (taxly-121300)*0.36 if married==0 & taxly>= 121300& taxly<263750 & kids==0
replace taxsim =(24000)*0.15+(58150-24000)*0.28 + (121300-58150)*0.31 + (263750-121300)*0.36 + (taxly-263750)*0.396 if married==0 & taxly>= 263750 & kids==0

*unmarried w/ head of household filing status, head taxed alone:
replace taxsim =max((taxly)*0.15,0) if married==0 & taxly<=32150 & kids>0 & kids>0 & kids != .
replace taxsim =(32150)*0.15+(taxly-32150 )*0.28 if married==0 & taxly>=32150& taxly<= 83050 & kids>0 & kids != .
replace taxsim =(32150)*0.15+(83050-32150)*0.28 + (taxly-83050)*0.31 if married==0 & taxly >= 83050& taxly<134500 & kids>0 & kids != .
replace taxsim =(32150)*0.15+(83050-32150)*0.28 + (134500-83050)*0.31 + (taxly-134500)*0.36 if married==0 & taxly>= 134500& taxly<263750 & kids>0 & kids != .
replace taxsim =(32150)*0.15+(83050-32150)*0.28 + (134500-83050)*0.31 + (263750-134500)*0.36 + (taxly-263750)*0.396 if married==0 & taxly>= 263750 & kids>0 & kids != .

*Include "partner's" tax obligation, separately computed:
replace taxsim =taxsim + max((taxwly)*0.15,0) if married==0 & taxwly<=24000
replace taxsim =taxsim +(24000)*0.15+(taxwly-24000)*0.28 if married==0 & taxwly>=24000& taxwly<= 58150 
replace taxsim =taxsim +(24000)*0.15+(58150-24000)*0.28 + (taxwly-58150)*0.31 if married==0 & taxwly >= 58150& taxwly<121300
replace taxsim =taxsim +(24000)*0.15+(58150-24000)*0.28 + (121300-58150)*0.31 + (taxwly-121300)*0.36 if married==0 & taxwly>= 121300& taxwly<263750
replace taxsim =taxsim +(24000)*0.15+(58150-24000)*0.28 + (121300-58150)*0.31 + (263750-121300)*0.36 + (taxwly-263750)*0.396 if married==0 & taxwly>= 263750



*Including child tax credit
replace taxsim = taxsim - 400*kids if married==1
*nonrefundable
replace taxsim = 0 if taxsim < 0

*Including FICA taxes:
replace taxsim = taxsim+0.0765*ly if ly <= 62700
replace taxsim = taxsim+0.062*62700 + 0.0145*ly if ly > 62700
replace taxsim = taxsim+0.0765*wly if wly <= 62700
replace taxsim = taxsim+0.062*62700 + 0.0145*wly if wly > 62700


*Including EITC:
replace taxsim = taxsim - totly*0.0765 if totly <= 4200 & (kids == 0 | married==0) 
replace taxsim = taxsim - 4200*0.0765 if totly > 4200 & totly <= 5300 & (kids == 0 | married==0) 
replace taxsim = taxsim - 4200*0.0765 + 0.0765*(totly - 5300) if totly <= 9500 & totly > 5300 & (kids == 0 | married==0)

replace taxsim = taxsim - totly*0.34 if kids == 1 & totly <= 6350 & married==1
replace taxsim = taxsim - 6350*0.34 if kids == 1 & totly >= 6350 & totly<=11650
replace taxsim = taxsim - 6350*0.34 + (totly-11650)*0.1598 if kids == 1 & totly >=11650 & totly<=25100

replace taxsim = taxsim - totly*0.4 if kids >= 2 & totly <= 8890
replace taxsim = taxsim - 8890*0.4 if kids >= 2 & totly >= 8890 & totly<=11650
replace taxsim = taxsim - 8890*0.4 + (totly-11650)*0.2106 if kids >= 2 & totly >=11650 & totly<=28495

gen y_posttax = y - taxsim
gen lss_posttax = ly + wly +soc_sec - taxsim

***************TAX SIMULATION COMPLETE***************
ren hourw sp_hours

*cleaning workyear measures, updating with work history over PSID:
replace workyears = . if workyears == 0 | workyears == 99
replace spworkyears = . if spworkyears == 0 | spworkyears == 99
replace workyears_full = . if workyears_full == 99
replace spworkyears_full = . if  spworkyears_full == 99

sort person year
tokenize `""" "sp_""'
foreach var in workyears spworkyears {
gen simwork = .
gen simworkfull = .
bysort `1'person: gen workupdatenum = sum(`var' != `var'[_n-1])
bysort `1'person: replace simwork = (year - year[_n-1])*(`1'hours>0 & `1'hours !=.) if `var' == `var'[_n-1]
bysort `1'person: replace simworkfull = (year - year[_n-1])*(`1'hours>1500 & `1'hours !=.) if `var' == `var'[_n-1]
bysort `1'person workupdatenum: gen temp = sum(simwork)
bysort `1'person workupdatenum: gen tempfull = sum(simworkfull)
replace `var' = `var' + temp
replace `var'_full = `var'_full+tempfull
drop workupdatenum simwork temp tempfull simworkfull
macro shift
}

gen head_elig = workyears >=10 ///
| (age < 24 & workyears >=1.5) ///
| (age>=24 & age <31 & workyears >= (age-21)/2) ///
| (age >= 31 & age<=42 & workyears >= 5) ///
| (age >42 & workyears >= (age-42)/4+5)

gen spouse_elig = spworkyears >=10 ///
| (agew < 24 & spworkyears >=1.5) ///
| (agew>=24 & agew <31 & spworkyears >= (agew-21)/2) ///
| (agew >= 31 & agew<=42 & spworkyears >= 5) ///
| (agew >42 & spworkyears >= (agew-42)/4+5)



*Identifying sample persons:
*sampleperson = 1 is an interviewed sample person
*sampleperson = 2 is an interviewed non-sample person and over age 64
*sampleperson = 0 this individual was nonsample and under age 64 (89-91)
*sampleperson = 5 this individual was not eligible to be followed (92 onward)
*in 1991: sampleperson == 1, 2, 4, 6 are all sample individuals
*In 1990, sampleperson==4 are sample individuals
gen insample_head = sampleperson != 0 & sampleperson != 5
replace insample_head = . if sampleperson == .
gen insample_head2 = samplecouple != 4 & samplecouple != 6 & samplecouple != 7
replace insample_head2 = . if samplecouple ==.
replace insample_head = insample_head2 if insample_head == .
drop insample_head2

*identifying childhood health:
egen chd_maxhealth = max(chd_health_head*(chd_health_head<=5)), by(person)

*States with temporary DI programs, comparable to SSDI
* CA, RI, NJ
gen tempDIstate =  state == 4 |  state == 38 | state == 29
bysort person: egen tempDIstate2 = max(tempDIstate)
drop tempDIstate
rename tempDIstate2 tempDIstate
*NY has one, but with very low payments.


********IMPUTING CONSUMPTION***********

*IMPUTING CONSUMPTION:

sort year
merge year using "$data\natpr"
tab _merge
drop if _merge!=3
drop _merge

gen pf   =pfood/100
gen lq  =ln((totfood)/pf)
gen lp  =ln(pf)

gen kidcat=recode(kids,0,1,2,4)
tab kidcat,gen(kidd)	

sort year
merge year using "$data\finprice"
keep if _merge==3
drop _merge

gen lpalc=ln(palc/100) 
gen lpfut=ln(pfutil/100)
gen lptra=ln(ptran/100)

merge using "$data\b0"
drop _merge


tab region, gen(regd)
tab educ, gen(edd)
gen age2 =age^2

*line up year and cohort dummies
forvalues i=1(1)28{
gen yrd`i' = year == `i' + 1989

}
gen coh = floor(birthy/5)*5
forvalues i=1(1)14{
gen cohd`i' = coh==1920+`i'*5
}

ren fsize ncomp
gen ncomp2=ncomp^2


cap program drop doit
program def doit
        local i=0
        while `i' < 1 {
		#delimit;
		gen lc`i'=(lq-(
		b`i'_age[1]    *age+
		b`i'_age2[1]   *age2+
		
		b`i'_lp[1]     *lp+
		b`i'_lpalc[1]  *lpalc+
		b`i'_lpfut[1]  *lpfut+
		b`i'_lptra[1]  *lptra+
		
		b`i'_cohd1[1]  *cohd1+
		b`i'_cohd2[1]  *cohd2+
		b`i'_cohd3[1]  *cohd3+
		b`i'_cohd4[1]  *cohd4+
		b`i'_cohd5[1]  *cohd5+
		b`i'_cohd6[1]  *cohd6+
		b`i'_cohd7[1]  *cohd7+
		b`i'_cohd8[1]  *cohd8+
		b`i'_cohd9[1]  *cohd9+
		b`i'_cohd10[1]  *cohd10+
		b`i'_cohd11[1]  *cohd11+
		b`i'_cohd12[1]  *cohd12+
		b`i'_cohd13[1]  *cohd13+
		b`i'_cohd14[1]  *cohd14+
		b`i'_edd2[1]   *edd2+
		b`i'_regd1[1]  *regd1+ 
		b`i'_regd2[1]  *regd2+ 
		b`i'_regd3[1]  *regd3+ 
		b`i'_kid2[1]   *kidd2+
		b`i'_kid3[1]   *kidd3+
		b`i'_kid4[1]   *kidd4+
		
		b`i'_married[1]   *married+
		
		b`i'_ncomp[1]  *ncomp+ 
		b`i'_dumwh[1]  *white+
		
		b`i'_cons[1]  ))
		/(b`i'_lx[1]+b`i'_lxed2[1]*edd2+ b`i'_lxmar[1]*married+
		b`i'_lxkid2[1]*kidd2+b`i'_lxkid3[1]*kidd3+b`i'_lxkid4[1]*kidd4
		+b`i'_lxy1[1]*yrd1+b`i'_lxy2[1]*yrd2+b`i'_lxy3[1]*yrd3+
		b`i'_lxy4[1]*yrd4+b`i'_lxy5[1]*yrd5+b`i'_lxy6[1]*yrd6+b`i'_lxy7[1]*yrd7
		+b`i'_lxy8[1]*yrd8+b`i'_lxy9[1]*yrd9+b`i'_lxy10[1]*yrd10+b`i'_lxy11[1]*y
rd11+b`i'_lxy12[1]*yrd12
		+b`i'_lxy13[1]*yrd13+b`i'_lxy14[1]*yrd14+b`i'_lxy15[1]*yrd15+b`i'_lxy16[1]*yrd16+b`i'_lxy17[1]*yrd17
		+b`i'_lxy18[1]*yrd18+b`i'_lxy19[1]*yrd19+b`i'_lxy20[1]*yrd20+b`i'_lxy21[1]*yrd21+b`i'_lxy22[1]*yrd22
		+b`i'_lxy23[1]*yrd23+b`i'_lxy24[1]*yrd24+b`i'_lxy25[1]*yrd25+b`i'_lxy26[1]*yrd26+b`i'_lxy27[1]*yrd27
		+b`i'_lxy28[1]*yrd28);
		#delimit cr
        local i=`i'+1               
        }
end

doit

replace lc = . if coh<1925 | year < 1990

rename lc lc_impute

ren ncomp fsize 
*************CONSUMPTION IMPUTATION COMPLETE************

replace hinscoverage = . if hinscoverage==0 | hinscoverage==8 | hinscoverage==9
replace hinscoverage = 0 if hinscoverage==5

drop sp_lw
gen nly = ly
gen nwly = wly



gen SSIrec = tr_ssi>1 & tr_ssi!=.
gen anyDI = SSIrec==1 | DI == 1
replace anyDI = . if DI == .

drop *_acc

****Adjusting health information ************************

foreach x of varlist arthritis asthma bloodp cancer diabetes emotion heart_att heart_dis lung_dis stroke {
replace `x' = `x' == 1 if `x' != .
}
foreach x of varlist trouble_meals trouble_shop trouble_money trouble_phone trouble_heavywork trouble_lightwork {
replace `x' = `x' == 1 | (`x' == 7 &  `x'_health == 1) if `x' != .
drop `x'_health
}


*cleaning health meassures:
replace trouble_walk = trouble_walk==1 if trouble_walk != .
replace trouble_bend = trouble_bend==1 if  trouble_bend!=.
replace trouble_drive = trouble_drive==1 if trouble_drive!=.
replace trouble_eye = trouble_eye==1 if trouble_eye!=.


replace in_hosp = in_hosp==1 if in_hosp!=.
replace days_in_hosp = 0 if days_in_hosp==999

replace height=. if height==99
replace bodyweight=. if bodyweight==999
gen BMI=(bodyweight*703)/height^2
gen obese = BMI > 30 if BMI != .
gen underw=BMI<18.5 if BMI != .

replace exerc = . if exerc==99.9

foreach x in arthritis asthma bloodp cancer diabetes emotion heart_att heart_dis lung_dis stroke {
replace `x' = `x'==1 if `x' != .
}

save "$temp\Full_Panel_1.dta", replace

*checked these vars for consistency:
*DI hours y trhw nonhy ly wly sp_hours soc_sec lw tr_fs troth tyoth wifechange wcomp wcompw uihead uiwife tr_tanf tr_ssi tr_welf tr_child tr_tanfw tr_ssiw tr_welfw tr_hhelp tr_whelp tr_childw  totfood rent fsize
*spending w_with_h w_no_h w_no_bus totfood health education util childcare transport rent homeinsure home_equity bus_ass cash real_estate stocks vehicles other_ass ira other_debt
*Only difference on main outcomes is in uihead and uiwife because they weren't 
*corrected for reference period in original code.
*Some differences on spending mesures in later years, but not apparently because I now code them wrong.
*reason: at least for medical expenses, jack's code didn't correct for time frame of question
*For old code: differences are only due to price inflation.
*Exception: childcare, for which I pull extra years of data.
/*
Code:
ren * *_new
ren id_new id
ren year_new year

merge 1:1 id year using "D:\Dropbox\DynamicDI\Low and Pistaferri\Code LP AER_Empirical\data_8608_final.dta"
*Missing households in the merge are mostly headed by female (sex_new == 2) 
*and who have different values of missing panel data (age, race, education, state)
*I tried matching based only on 2008 info, still I have too many guys...

ren SSI tr_ssi

foreach x in DI hours y trhw nonhy ly wly sp_hours soc_sec lw tr_fs troth tyoth wifechange wcomp wcompw uihead uiwife tr_ssi tr_hhelp tr_whelp totfood rent fsize {
fsum `x' `x'_new if _merge==3
}

foreach x in spending w_with_h w_no_h w_no_bus totfood health education util childcare transport rent homeinsure home_equity bus_ass cash stocks vehicles other_ass ira other_debt {
fsum `x' `x'_new if _merge == 3
}



*/
cd "$programs"
